// pages/refund_detail_page/refund_detail_page.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getVisitOrder, getArrivalOrder, cancelOrder, refundOrder, finishOrder, getOrderStep } from '../../static/api/api.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        refund_remark:'',
        refund_price:'',
        uploadImg: [],
        total_payment:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            order_type: options.order_type,
            order_id: options.order_id
        })
        if (this.data.order_type == 10) {
            this.getVisitOrder()
        } else {
            this.getArrivalOrder()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    inputValueChange(e) {
        utils.inputValueChange(e, this)
    },
    chooseImage(e) {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
            sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
            success: res => {
                console.log(res.tempFilePaths)
                this.uploadImg(res.tempFilePaths[0])

            }
        })
    },
    //预览图片
    handleImagePreview(e) {
        const idx = e.target.dataset.index
        const images = this.data.uploadImg
        wx.previewImage({
            current: images[idx],  //当前预览的图片
            urls: images,  //所有要预览的图片
        })
    },
    //删除图片
    delImage(e) {
        let dataset = e.currentTarget.dataset;
        console.log(dataset)
        let images = this.data.uploadImg;
        images.splice(dataset.index, 1)
        this.setData({
            uploadImg: images
        })
    },
    //上传图片
    uploadImg(path) {
        let that = this;
        console.log(path)
        setTimeout(function () {
            var urls = config.interfaceHost + "/api.php/common/Qiniu/upLoadImg";
            console.log(path);
            wx.showLoading({
                title: '加载中..',
            })
            wx.uploadFile({
                url: urls,
                filePath: path,
                name: 'image',
                header: {
                    'content-type': 'multipart/form-data',
                    'site': config.site,
                    'os': config.os,
                    'token': wx.getStorageSync('token')
                },
                success: function (res) {
                    wx.hideLoading()
                    var str = res.data;
                    that.data.uploadImg.push(JSON.parse(res.data).data.url)
                    let images = that.data.uploadImg
                    console.log(images)
                    // 限制最多只能留下3张照片
                    that.setData({
                        uploadImg: images.length <= 3 ? images : images.slice(0, 3)
                    })
                },
                fail: function (res) {
                    wx.hideLoading()
                    console.log(res);
                    wx.showModal({
                        title: '提示',
                        content: "网络请求失败，请确保网络是否正常",
                        showCancel: false,
                        success: function (res) {

                        }
                    });
                    wx.hideToast();
                }
            });
        }, 500)
    },
    //用户退款
    //用户退款
    refundOrder(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        if (!this.data.refund_remark){
            wx.showToast({
                icon:'none',
                title: '请输入退款原因',
            })
            return;
        }
        let params = {
            order_id: this.data.order_data.order_id,
            remark: this.data.refund_remark,
            main_img: this.data.uploadImg.join(',')
        }
        wx.showModal({
            title: '提示',
            content: '确定要申请退款吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    refundOrder(params).then(response => {
                        wx.hideLoading()
                        wx.showToast({
                            title: '申请退款成功',
                        })
                        setTimeout(function () {
                            let pages = getCurrentPages();
                            let tarPage = pages[pages.length - 2];
                            tarPage.setData({
                                status: '65',
                            })
                            wx.navigateBack({

                            })
                        }, 1000)


                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    },
    //获取上门订单详情
    getVisitOrder() {
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getVisitOrder(options).then(response => {
            wx.hideLoading();
            this.setData({
                'order_data': response.data,
                'total_payment': (parseFloat(response.data.payment_amount) + parseFloat(response.data.seconds_price)).toFixed(2)
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    //获取到店订单详情
    getArrivalOrder() {
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getArrivalOrder(options).then(response => {
            wx.hideLoading();
            this.setData({
                'order_data': response.data,
                'total_payment': response.data.payment_amount
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    
})